package com.proj.hot.animation;

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewPropertyAnimator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

/**
 * 动画封装类
 * Created by zangchenggang on 16/5/23.
 */
public class AnimFactory {
    private static final int SCALE_DURATION = 100;
    private static final int TRANSLATE_DURATION = 200;

    /**
     * 缩放动画,用于缩放控件
     *
     * @param startScale 控件的起始尺寸倍率
     * @param endScale   控件的终点尺寸倍率
     * @return
     */
    public static Animation zoomAnimation(float startScale, float endScale, int duration) {
        ScaleAnimation anim = new ScaleAnimation(startScale, endScale, startScale, endScale,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setFillAfter(true);
        anim.setDuration(duration);
        return anim;
    }

    /**
     * 放大动画，用于控件的放大
     *
     * @return 从原始大小缩放为原来的1.1倍
     */
    public static Animation zoomInAnimation() {
        return zoomAnimation(1.0f, 1.1f, SCALE_DURATION);
    }

    /**
     * 缩小动画，用于控件的缩小
     *
     * @return 从原来的1.1倍缩放为原始大小
     */
    public static Animation zoomOutAnimation() {
        return zoomAnimation(1.1f, 1.0f, SCALE_DURATION);
    }

    public static Animation translateAnimation(float fromXValue, float toXValue, float fromYValue,
                                               float toYValue) {
        TranslateAnimation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, fromXValue,
                Animation.RELATIVE_TO_SELF, toXValue, Animation.RELATIVE_TO_SELF, fromYValue,
                Animation.RELATIVE_TO_SELF, toYValue);
        anim.setFillAfter(true);
        anim.setInterpolator(new DecelerateInterpolator());
        anim.setDuration(150);
        return anim;
    }

    /**
     * 呼吸效果动画
     *
     * @param view
     */
    public static void breathingAnimation(View view) {
        AlphaAnimation animation = new AlphaAnimation(1, 0.6f);
        animation.setStartOffset(200);
        animation.setDuration(1500);
        animation.setRepeatCount(Integer.MAX_VALUE);
        animation.setRepeatMode(Animation.REVERSE);
        view.startAnimation(animation);
    }

    /**
     * XXXXXX
     */
    public static void frameTranslateAnim(View view, ImageView imageView) {
        //设置border的边框的长宽
        int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(width, height);
        int mheight = view.getMeasuredHeight();
        int mwidth = view.getMeasuredWidth();
        ViewGroup.LayoutParams params;
        params = imageView.getLayoutParams();
        params.height = mheight;
        params.width = mwidth;
        imageView.setLayoutParams(params);
        //设置border的移动位置
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        ViewPropertyAnimator animator;
        animator = imageView.animate();
        animator.setDuration(TRANSLATE_DURATION);
        animator.x(location[0]);
        animator.y(location[1]);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.start();
    }

    /**
     * flyBorder的统一动画
     *
     * @param v      目标view
     * @param border 移动的image
     * @param h      差距高
     * @param w      差距宽
     * @param x      差距x
     * @param y      差距y
     */
    public static void flyBorderAnim(View v, ImageView border, int h, int w, int x, int y) {
        //设置border的边框的长宽
        int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        v.measure(width, height);
        int mheight = v.getMeasuredHeight() + h;
        int mwidth = v.getMeasuredWidth() + w;
        ViewGroup.LayoutParams params;
        params = border.getLayoutParams();
        params.height = mheight;
        params.width = mwidth;
        border.setLayoutParams(params);
        //设置border的移动位置
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        ViewPropertyAnimator animator;
        animator = border.animate();
        animator.setDuration(TRANSLATE_DURATION);
        animator.x(location[0] + x);
        animator.y(location[1] + y);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.start();

    }
}
